/***************************************************************************
 创建者: 华磊
 开始时间: 2019.4.27
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写
 ***************************************************************************/

#include "programsimplecheck.h"
#include "instructionEnum.h"

ProgramSimpleCheck::ProgramSimpleCheck()
{
    initialInstructionSimpleRegExp();
}

int ProgramSimpleCheck::initialInstructionSimpleRegExp()
{
    QString vague_str_motion;
    QString vague_str_wait;
    QString vague_str_jump;
    QString vague_str_reg;
    QString vague_str_io;
    QString vague_str_label;
    QString vague_str_run;
    QString vague_str_select;
    QString vague_str_if;
    QString vague_str_call;
    QString vague_str_for;
    QString vague_str_endfor;
    QString vague_str_coordinate;
    QString vague_str_anotation;
    QString vague_str_motion_skip;
    QString vague_str_pause;
    QString vague_str_abort;
    QString vague_str_end;
    QString vague_str_ualm;
    QString vague_str_message;
    QString vague_str_override;
    QString vague_str_timer;
    QString vague_str_rsr;

    QString vague_str_gripper_active;
    QString vague_str_gripper_set;
    QString vague_str_gripper_wait;
    QString vague_str_gripper;

     QString vague_str_modbusStart;
     QString vague_str_modbusRead;
     QString vague_str_modbusWrite;

     QString vague_str_moveMag;
     QString vague_str_moveFree;

     QString vague_str_tcpStart;
     QString vague_str_tcpRead;
     QString vague_str_tcpWrite;

     QString vague_str_music;

     /**
      * @brief vague_str_setOdom        SET_ODOM重置里程计指令
      */
     QString vague_str_setOdom;

     /**
      * @brief vague_str_charging       CHARGING  充电指令
      */
     QString vague_str_charging;

     /**
      * @brief vague_str_liftMechanism  LIFT 升降机构指令
      */
     QString vague_str_liftMechanism;

     /**
      * @brief vague_str_loadMap        LOAD_MAP加载地图指令
      */
     QString vague_str_loadMap;

     //电量检测指令
     QString vague_str_checkBattery;
     //等待呼叫指令
     QString vague_str_waitCommand;

     /**
      * @brief vague_str_robotMove      机械手控制指令
      */
     QString vague_str_robotMove;

     /**
      * @brief vague_str_roller         滚筒指令
      */
     QString vague_str_roller;

     QString visionRun_pattern;
     QString visionClearData_pattern;
     QString visionGetData_pattern;
     QString visionTrigger_pattern;

     QString trackStart_pattern;
     QString trackEnd_pattern;
     QString trackGetData_pattern;

     /******************************************************************************
      * 日期：2018年10月26日
      * 修改人：丁培
      * 修改内容: 指令匹配 vague_str_reg　添加[ 匹配
      *          vague_str_motion vague_str_wait  结尾　\\s*　修改为　\\s+
      *          vague_str_jump vague_str_if vague_str_call 结尾　\\s*　修改为　\\s+
      * **************************************************************************/
     // MOVE_FREE
     vague_str_motion = "^\\s*([JLCA])\\s+";
     vague_str_wait = "^\\s*(WAIT)\\s+";
     vague_str_jump = "^\\s*(JMP)\\s+";
     vague_str_reg = "^\\s*([RPLS])(\\[)";
     vague_str_io = "^\\s*([DIORAG]{2,3})(\\[)";
     vague_str_label = "^\\s*(LBL)(\\[)";
     vague_str_run = "^\\s*(RUN)\\s+";//不要用^\\s*(RUN)\\s*
     vague_str_select = QString("%1|%2|%3").arg("^\\s*(SELECT)\\s*").arg("^\\s*(=)\\s*").arg("^\\s*(ELSE)\\s*");
     vague_str_if = "^\\s*(IF)\\s+";
     vague_str_call = "^\\s*(CALL)\\s+";
     vague_str_for = "^\\s*(FOR)\\s+";
     vague_str_endfor = "^\\s*(ENDFOR)\\s*";
     vague_str_coordinate = QString("%1|%2").arg("^\\s*(UTOOL)\\s*").arg("^\\s*(UFRAME)\\s*");
     vague_str_anotation ="^\\s*(//)\\s*";
     vague_str_motion_skip = "";
     vague_str_pause = "^\\s*(PAUSE)\\s*";
     vague_str_abort = "^\\s*(ABORT)\\s*";
     vague_str_end = "^\\s*(END)\\s*";
     vague_str_ualm = "^\\s*(UALM)\\s*";
     vague_str_message = "^\\s*(MESSAGE)\\s*";
     vague_str_override = "^\\s*(OVERRIDE)\\s*";
     vague_str_timer = "^\\s*(TIMER)\\s*";
     vague_str_rsr = "^\\s*(RSR)\\s*";

     vague_str_modbusStart = "^\\s*(MODBUS_START)\\s*";
     vague_str_modbusRead = "^\\s*(MODBUS_READ)\\s*";
     vague_str_modbusWrite = "^\\s*(MODBUS_WRITE)\\s*";

     vague_str_moveMag = "^\\s*(MOVE_MAG)\\s*";
     vague_str_moveFree = "^\\s*(MOVE_FREE)\\s*";

     vague_str_tcpStart = "^\\s*(TCP_START)\\s*";
     vague_str_tcpRead = "^\\s*(TCP_READ)\\s*";
     vague_str_tcpWrite = "^\\s*(TCP_WRITE)\\s*";

     // MUSIC
     vague_str_music = "^\\s*(MUSIC)\\s*";

     // SET_ODOM
     vague_str_setOdom = "^\\s*(SET_ODOM)\\s*";

     // CHARGING
     vague_str_charging = "^\\s*(CHARGING)\\s*";

     // LIFT
     vague_str_liftMechanism = "^\\s*(LIFT)\\s*";

     // LOAD_MAP
     vague_str_loadMap = "^\\s*(LOAD_MAP)\\s*";

     // CHECKBATTERY 电量检测指令
     vague_str_checkBattery = "^\\s*(CHECK_BATTERY)\\s+";

     // WAIT_COMMAND 等待呼叫指令
     vague_str_waitCommand = "^\\s*(WAIT_COMMAND)\\s+";

     // ROBOT_MOVE机械手控制
     vague_str_robotMove = "^\\s*(ROBOT_MOVE)\\s+";

     // 滚筒指令
     vague_str_roller = "^\\s*(ROLLER)\\s+";

     visionRun_pattern = "^\\s*(RUNVISION)(\\[)";
     visionClearData_pattern = "^\\s*(CLEARVISIONDATA)(\\[)";
     visionGetData_pattern = "^\\s*(GETVISIONDATA)(\\[)";
     visionTrigger_pattern = "^\\s*(TRIGGERVISION)(\\[)";

     trackStart_pattern = "^\\s*(TRACKSTART)(\\[)";
     trackEnd_pattern = "^\\s*(TRACKEND)(\\[)";
     trackGetData_pattern = "^\\s*(GETTRACKDATA)(\\[)";

     vague_str_gripper_active = "^\\s*(ActiveGripper)\\s*";
     vague_str_gripper_set ="^\\s*(SetGripper)\\s*";
     vague_str_gripper_wait = "^\\s*(WaitGripper)\\s*";
     vague_str_gripper = QString("%1|%2|%3").arg(vague_str_gripper_active).arg(vague_str_gripper_set).arg(vague_str_gripper_wait);


    //     // MODBUS_START
    //     QString modbusStart_pattern(vague_str_modbusStart);
    //     // MODBUS_RAED
    //     QString modbusRead_pattern(vague_str_modbusRead);
    //     // MODBUS_WRITE
    //     QString modbusWrite_pattern(vague_str_modbusWrite);

    //     // MOVE_MAG
    //     QString moveMag_pattern(vague_str_moveMag);
    //     // MOVE_FREE
    //     QString moveFree_pattern(vague_str_moveFree);
    //     // WAIT_COMMAND
    //     QString waitCommand_pattern(vague_str_waitCommand);

    //     QString tcpStart_pattern(vague_str_tcpStart);

    //     QString tcpRead_pattern(vague_str_tcpRead);

    //     QString tcpWrite_pattern(vague_str_tcpWrite);

    //     // MUSIC
    //     QString music_pattern(vague_str_music);

    //     // SET_ODOM
    //     QString setOdom_patern(vague_str_setOdom);

    //     // CHARGING
    //     QString charging_pattern(vague_str_charging);

    //     // LIFT
    //     QString lift_pattern(vague_str_liftMechanism);

    //     QString loadMap_pattern(vague_str_loadMap);

    //     QString checkBattery_pattern(vague_str_checkBattery);

    //     // ROBOT_MOVE
    //     QString robotMove_pattern(vague_str_robotMove);

    //     QString roller_pattern(vague_str_roller);

    //     //MOTION
    //     QString motion_pattern(vague_str_motion);
    //     //JUMP
    //     QString jmp_pattern(vague_str_jump);
    //     //LABEL
    //     QString label_pattern(vague_str_label);
    //     //IO
    //     QString io_pattern(vague_str_io);
    //     //CALL
    //     QString call_pattern(vague_str_call);
    //     //IF
    //     QString if_pattern(vague_str_if);
    //     //SELECT
    //     QString select_pattern(vague_str_select);
    //     //RUN
    //     QString run_pattern(vague_str_run);
    //     //FOR
    //     QString for_pattern(vague_str_for);
    //     QString endfor_pattern(vague_str_endfor);
    //     //REGISTER
    //     QString register_pattern(vague_str_reg);
    //     //PAL
    //     QString pal_pattern(vague_str_pal);
    //     //UFRAME UTOOL
    //     QString coordinate_pattern(vague_str_coordinate);
    //     //WAIT
    //     QString wait_pattern(vague_str_wait);
    //     //注释
    //     QString anotation_pattern(vague_str_anotation);

    //     //MOTION SKIP
    // //    QString motion_skip_pattern(str_rx_MOTION_SKIP_JLC);
    //     //PAUSE
    //     QString pause_pattern(vague_str_pause);
    //     //ABORT
    //     QString abort_pattern(vague_str_abort);
    //     //END
    //     QString end_pattern(vague_str_end);
    //     //UALM
    //     QString ualm_pattern(vague_str_ualm);
    //     //MESSAGE
    //     QString message_pattern(vague_str_message);
    //     //OVERRIDE
    //     QString override_pattern(vague_str_override);
    //     //TIMER
    //     QString timer_pattern(vague_str_timer);
    //     //RSR
    //     QString rsr_pattern(vague_str_rsr);
    //     //GRIP
    //     QString grip_pattern(vague_str_gripper);

    //MOTION
    rx_motion.setPattern(vague_str_motion);

    //JUMP
    rx_jmp.setPattern(vague_str_jump);
    //LABEL
    rx_label.setPattern(vague_str_label);
    //IO
    rx_io.setPattern(vague_str_io);
    //CALL
    rx_call.setPattern(vague_str_call);
    //IF
    rx_if.setPattern(vague_str_if);
    //SELECT
    rx_select.setPattern(vague_str_select);
    //RUN
    rx_run.setPattern(vague_str_run);
    //FOR
    rx_for.setPattern(vague_str_for);
    rx_endfor.setPattern(vague_str_endfor);
    //REGISTER
    rx_register.setPattern(vague_str_reg);
    rx_pal.setPattern("^\\s*(PALLETIZING)\\s*");
    //UFRAME UTOOL
    rx_coordinate.setPattern(vague_str_coordinate);
    //WAIT
    rx_wait.setPattern(vague_str_wait);
    //注释
    rx_anotation.setPattern(vague_str_anotation);
    rx_anotationExtFunc.setPattern("\\s*[!;].*");
    //MOTION_SKIP
//    rx_motion_skip(vague_str_motion_skip);
    //PAUSE
    rx_pause.setPattern(vague_str_pause);
    //ABORT
    rx_abort.setPattern(vague_str_abort);
    //END
    rx_end.setPattern(vague_str_end);
    //UALM
    rx_ualm.setPattern(vague_str_ualm);
    //MESSAGE
    rx_message.setPattern(vague_str_message);
    //OVERRIDE
    rx_override.setPattern(vague_str_override);
    //TIMER
    rx_timer.setPattern(vague_str_timer);
    //RSR
    rx_rsr.setPattern(vague_str_rsr);

    rx_gripper.setPattern(vague_str_gripper);
    // MODBUS_START
    rx_modbusStart.setPattern(vague_str_modbusStart);
    // MODBUS_READ
    rx_modbusRead.setPattern(vague_str_modbusRead);
    // MODBUS_WRITE
    rx_modbusWrite.setPattern(vague_str_modbusWrite);
    // MOVE_MAG
    rx_moveMag.setPattern(vague_str_moveMag);
    // MOVE_FREE
    rx_moveFree.setPattern(vague_str_moveFree);
    // WAIT_COMMANT
    rx_waitCommand.setPattern(vague_str_waitCommand);

    rx_tcpStart.setPattern(vague_str_tcpStart);
    rx_tcpRead.setPattern(vague_str_tcpRead);
    rx_tcpWrite.setPattern(vague_str_tcpWrite);

    rx_music.setPattern(vague_str_music);
    rx_setOdom.setPattern(vague_str_setOdom);

    rx_charging.setPattern(vague_str_charging);
    rx_lift.setPattern(vague_str_liftMechanism);

    rx_loadMap.setPattern(vague_str_loadMap);

    rx_checkBattery.setPattern(vague_str_checkBattery);

    rx_robotMove.setPattern(vague_str_robotMove);

    rx_roller.setPattern(vague_str_roller);

    rx_visionRun.setPattern(visionRun_pattern);
    rx_visionClearData.setPattern(visionClearData_pattern);
    rx_visionGetData.setPattern(visionGetData_pattern);
    rx_visionTrigger.setPattern(visionTrigger_pattern);

    rx_trackStart.setPattern(trackStart_pattern);
    rx_trackEnd.setPattern(trackEnd_pattern);
    rx_trackGetData.setPattern(trackGetData_pattern);

    rx_axisMove.setPattern("^\\s*(AXISMOVE)\\s+");

    rx_regPr.setPattern("^\\s*(PR)(\\[)");
    rx_regPl.setPattern("^\\s*(PL)(\\[)");

    rx_regWeldStart.setPattern("^\\s*Weld\\sStart.*");
    rx_regWeldEnd.setPattern("^\\s*Weld\\sEnd.*");
    rx_regWeave.setPattern("^\\s*Weave\\s*\\[\\d.*");
    rx_regWeaveEnd.setPattern("^\\s*Weave\\s*End.*");
    rx_regTrackWeldTast.setPattern("^\\s*Track\\sWeld\\sTAST.*");
    rx_regTrackWeldEnd.setPattern("^\\s*Track\\sWeld\\sEnd.*");
    rx_regWatchDI.setPattern("^\\s*Watch\\sDI\\[\\d+\\]\\=.*");

    rx_GRegister.setPattern("^\\s*GR\\[.*");

    rx_laserCameraFetchPoint.setPattern("^\\s*RECORD_LASERCAMERA.*");
    rx_laserCameraControl.setPattern("^\\s*LASERCAMERA_CTRL.*");
    rx_laserCameraTrack.setPattern("^\\s*LASERCAMERA_TRACK.*");
    rx_laserScan.setPattern("^\\s*LASERSCAN.*");
    rx_bufferMove.setPattern("^\\s*BUFFERMOVE.*");
    return 1;
}

int ProgramSimpleCheck::patExtractInstructionType(QString programStr)
{

//    if( programStr.contains("L P["))
//    {
//        return MOTION;
//    }
//    else if( programStr.contains("J P["))
//    {
//        return MOTION;
//    }
//    else
    if(0 == programStr.indexOf(rx_anotation))
    {
        return ANOTATION;
    }
    else if(0 == programStr.indexOf(rx_anotationExtFunc))
    {
        return ANOTATION;
    }
    else if( 0 == programStr.indexOf(rx_jmp))
    {
        return JUMP;
    }
    else if(0 == programStr.indexOf(rx_label))
    {
        return LABEL;
    }
    else if(0 == programStr.indexOf(rx_call))
    {
        return CALL;
    }
    else if(0 == programStr.indexOf(rx_select))
    {
        return SELECT;
    }
    else if(0 == programStr.indexOf(rx_wait))
    {
        return WAIT;
    }
    else if(0 == programStr.indexOf(rx_run))
    {
        return RUN;
    }
    else if((0 == programStr.indexOf(rx_for))
            || (0 == programStr.indexOf(rx_endfor)))
    {
        return FOR;
    }
    else if(0 == programStr.indexOf(rx_pal))
    {
        return INS_PALLET;
    }
    else if(0 == programStr.indexOf(rx_coordinate))
    {
        return COORDINATE;
    }
    else if(0 == programStr.indexOf(rx_gripper))
    {
        return GRIPPER;
    }
    else if(0 == programStr.indexOf(rx_if))
    {
        return IF;
    }
    else if(0 == programStr.indexOf(rx_pause))
    {
        return PAUSE;
    }
    else if(0 == programStr.indexOf(rx_abort))//会和圆弧指令冲突，２个都可以匹配
    {
        return ABORT;
    }
    else if(0 == programStr.indexOf(rx_end))
    {
        return END;
    }
    else if(0 == programStr.indexOf(rx_ualm))
    {
        return UALM;
    }
    else if(0 == programStr.indexOf(rx_message))
    {
        return MESSAGE;
    }
    else if(0 == programStr.indexOf(rx_override))
    {
        return OVERRIDE;
    }
    else if(0 == programStr.indexOf(rx_timer))
    {
        return TIMER;
    }
    else if(0 == programStr.indexOf(rx_rsr))
    {
        return  RSR;
    }
    else if((0 == programStr.indexOf(rx_register)) && !programStr.contains(rx_loadMap)
            && !programStr.contains(rx_setOdom))
    {
        if(programStr.contains("FINE")
                ||(programStr.contains("CNT")))
        {

            return MOTION;

        }
        else
        {
            return REG;
        }
    }
    else if(0 == programStr.indexOf(rx_motion) && !programStr.contains(rx_loadMap)
            && !programStr.contains(rx_charging))
    {

            return MOTION;

    }
    else if(0 == programStr.indexOf(rx_GRegister))
    {
        return REG;
    }
    else if(0 == programStr.indexOf(rx_modbusStart))
    {
        return MODBUS_START;
    }

    else if(0 == programStr.indexOf(rx_modbusRead))
    {
        return MODBUS_READ;
    }

    else if(0 == programStr.indexOf(rx_modbusWrite))
    {
        return MODBUS_WRITE;
    }
    else if(0 == programStr.indexOf(rx_moveMag))
    {
        return MOVE_MAG;
    }
    else if(0 == programStr.indexOf(rx_moveFree))
    {
        return MOVE_FREE;
    }
    else if(0 == programStr.indexOf(rx_tcpStart))
    {
        return TCP_START;
    }
    else if(0 == programStr.indexOf(rx_tcpRead))
    {
        return TCP_READ;
    }
    else if(0 == programStr.indexOf(rx_tcpWrite))
    {
        return TCP_WRITE;
    }
    else if(0 == programStr.indexOf(rx_music))
    {
        return MUSIC;
    }
    else if(0 == programStr.indexOf(rx_setOdom))
    {
        return SET_ODOM;
    }
    else if(0 == programStr.indexOf(rx_charging))
    {
        return CHARGING;
    }
    else if(0 == programStr.indexOf(rx_lift))
    {
        return LIFT;
    }
    else if(0 == programStr.indexOf(rx_loadMap))
    {
        return LOAD_MAP;
    }
    else if(0 == programStr.indexOf(rx_checkBattery))
    {
        return CHECK_BATTERY;
    }
    else if(0 == programStr.indexOf(rx_waitCommand))
    {
        return WAIT_COMAND;
    }
    else if(0 == programStr.indexOf(rx_robotMove))
    {
        return ROBOT_MOVE;
    }
    else if(0 == programStr.indexOf(rx_roller))
    {
        return ROLLER;
    }
    else if(0 == programStr.indexOf(rx_visionRun))
    {
        return INS_VISION_RUN;
    }
    else if(0 == programStr.indexOf(rx_visionTrigger))
    {
        return INS_VISION_TRIGGER;
    }
    else if(0 == programStr.indexOf(rx_visionGetData))
    {
        return INS_VISION_GET_DATA;
    }
    else if(0 == programStr.indexOf(rx_visionClearData))
    {
        return INS_VISION_CLEAR_DATA;
    }
    else if(0 == programStr.indexOf(rx_trackStart))
    {
        return INS_TRACK_START;
    }
    else if(0 == programStr.indexOf(rx_trackEnd))
    {
        return INS_TRACK_END;
    }
    else if(0 == programStr.indexOf(rx_trackGetData))
    {
        return INS_TRACK_GET_DATA;
    }
    else if(0 == programStr.indexOf(rx_axisMove))
    {
        return INS_AXIS_MOVE;
    }
    else if(0 == programStr.indexOf(rx_regPr))
    {
        return INS_REG_PR;
    }
    else if(0 == programStr.indexOf(rx_regPl))
    {
        return INS_REG_PL;
    }//拓展新指令Fundation增加代码20191122hualei
    else if(0 == programStr.indexOf(rx_regWeldStart))
    {
        return INS_WELD_START;
    }
    else if(0 == programStr.indexOf(rx_regWeldEnd))
    {
        return INS_WELD_END;
    }
    else if(0 == programStr.indexOf(rx_io))//表达式不严谨，ＧＲ会误判
    {
        return IO;
    }
    else if(0 == programStr.indexOf(rx_regWeave))
    {
        return INS_WEAVE;
    }
    else if(0 == programStr.indexOf(rx_regWeaveEnd))
    {
        return INS_WEAVE_END;
    }
    else if(0 == programStr.indexOf(rx_regTrackWeldTast))
    {
        return INS_TRACKWELD_TAST;
    }
    else if(0 == programStr.indexOf(rx_regTrackWeldEnd))
    {
        return INS_TRACKWELD_END;
    }
    else if(0 == programStr.indexOf(rx_regWatchDI))
    {
        return INS_WATCHDI;
    }
    else if(0 == programStr.indexOf(rx_laserCameraFetchPoint))
    {
        return INS_LASER_CAMERA_FETCHPOINT;
    }
    else if(0 == programStr.indexOf(rx_laserCameraControl))
    {
        return INS_LASER_CAMERA_CTRL;
    }
    else if(0 == programStr.indexOf(rx_laserCameraTrack))
    {
        return INS_LASER_CAMERA_TRACK;
    }
    else if(0 == programStr.indexOf(rx_laserScan))
    {
        return INS_LASER_SCAN;
    }
    else if(0 == programStr.indexOf(rx_bufferMove))
    {
        return INS_BUFFER_MOVE;
    }
    else
    {
//        MessageLog::getInstance()->addMessage( ENUM_MSG_REMIND, "ProgramEngine", "ProgramEngine",  3002, robotId , 0, 0);
        return ERR;
    }
}
